package com.bhprod.comatule.search;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bhprod.comatule.R;
import com.bhprod.comatule.db.DBDataSource;
import com.bhprod.comatule.model.Species;
import com.bhprod.comatule.slideshow.SlideshowActivity;

public class SearchBySpeciesActivity extends Activity {
	
	// TODO Vérifier qu'il n'y a pas le mm bug que pour les taxons sur les checkboxes

	private SpeciesListAdapter adapter;
	private String nbGroupsSuffix;
	private int nbSelectedGroups = 0;
	private List<Integer> selectedGroupPositionInCurrentLevel = new ArrayList<Integer>();

	/**
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_group_list);

		ImageButton listButton = (ImageButton) findViewById(R.id.buttonGroupList);
		listButton.setVisibility(View.INVISIBLE);
		
		TextView levelText = (TextView) findViewById(R.id.listLevel);
		levelText.setVisibility(View.INVISIBLE);

		nbGroupsSuffix = getString(R.string.nb_selected_species);

		Bundle b = getIntent().getExtras();
		ArrayList<Integer> speciesIds = b.getIntegerArrayList("speciesIds");

		ExpandableListView listView = (ExpandableListView) findViewById(R.id.groupsList);

		adapter = new SpeciesListAdapter(this);
		listView.setAdapter(adapter);

		// Get the list of species
		List<Species> species = DBDataSource.getInstance().getSpeciesByIds(speciesIds);
		adapter.setSpecies(species);
		adapter.notifyDataSetChanged();

		updateNbGroups();
	}

	/**
	 * Manage click on checkboxes
	 * 
	 * @param checkbox
	 *            The clicked checkbox
	 */
	public void checkGroup(View checkbox) {
		CheckBox cb = (CheckBox) checkbox;
		if (cb.isChecked()) {
			nbSelectedGroups++;
			selectedGroupPositionInCurrentLevel.add((Integer) cb.getTag());
		} else {
			nbSelectedGroups--;
			selectedGroupPositionInCurrentLevel.remove((Integer) cb.getTag());
		}
		updateNbGroups();
	}

	public void clickPhotoButton(View button) {
		if (selectedGroupPositionInCurrentLevel.size() <= 0) {
			return;
		}

		// Search selected species
		ArrayList<Integer> speciesIds = new ArrayList<Integer>();

		List<Species> species = adapter.getSpecies();

		for (Integer speciesPosition : selectedGroupPositionInCurrentLevel) {
			speciesIds.add(species.get(speciesPosition).getId());
		}

		Intent intent = new Intent(this, SlideshowActivity.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("speciesIds", speciesIds);
		intent.putExtras(b);
		startActivity(intent);
	}

	/**
	 * Update the displayed number of selected groups
	 */
	private void updateNbGroups() {
		TextView nbGroups = (TextView) findViewById(R.id.nbGroups);
		nbGroups.setText(Integer.toString(nbSelectedGroups) + " "
				+ nbGroupsSuffix);
	}

}
